home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / GEOD.ZIP / geod.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-19  |  8.7 KB  |  259 lines

  1. #define INCL_WIN
  2.  
  3. #include <os2.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include "geod.h"
  8.  
  9. /****** Declarations ******/
  10.  
  11. MRESULT EXPENTRY MainDlgProc(HWND, ULONG, MPARAM, MPARAM);
  12. MRESULT EXPENTRY AboutDlgProc(HWND, ULONG, MPARAM, MPARAM);
  13. VOID CenterWindow(HWND);
  14. size_t commafmt(CHAR *, INT, LONG);
  15.  
  16. #define NUL '\0'
  17.  
  18. /****** Globals *******/
  19.  
  20. HAB   hab;
  21. HWND  hwndFrame, hwndMenu;
  22. CHAR  szBuff[DEF_BUFF];
  23.  
  24. /****** Main Procedure ******/
  25.  
  26. int main(void)
  27. {
  28.     HMQ   hmq;
  29.     QMSG  qmsg;
  30.  
  31.     hab = WinInitialize(0);
  32.     hmq = WinCreateMsgQueue(hab, 0);
  33.  
  34.     hwndFrame = WinLoadDlg(HWND_DESKTOP, HWND_DESKTOP,
  35.                            MainDlgProc, 0, GEOD_DIALOG, NULL);
  36.  
  37.     hwndMenu = WinLoadMenu(hwndFrame, 0, GEOD_DIALOG);
  38.     WinSendMsg(hwndFrame, WM_UPDATEFRAME, 0L, 0L);
  39.  
  40.     while (WinGetMsg(hab, &qmsg, 0, 0, 0))
  41.         WinDispatchMsg(hab, &qmsg);
  42.  
  43.     WinDestroyWindow(hwndFrame);
  44.     WinDestroyMsgQueue(hmq);
  45.     WinTerminate(hab);
  46.  
  47.     return 0;
  48. }
  49.  
  50. MRESULT EXPENTRY MainDlgProc(HWND hWnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  51. {
  52.     BOOL    bHandled = TRUE;
  53.     MRESULT mReturn  = 0;
  54.     double  xFrac, xInt;
  55.     CHAR    xBuff[DEF_BUFF], FracBuff[10];
  56.     static USHORT usMeasure = GEOD_NM, usEllipsoid = GEOD_AIRY;
  57.  
  58.     switch (msg)
  59.     {           
  60.         case WM_INITDLG:
  61.             CenterWindow(hWnd);
  62.             WinLoadString(hab, 0, GEOD_NM, DEF_BUFF, szBuff);
  63.             WinSetWindowText(WinWindowFromID(hWnd, GEOD_CURRMEASURE), szBuff);
  64.             WinLoadString(hab, 0, GEOD_AIRY, DEF_BUFF, szBuff);
  65.             WinSetWindowText(WinWindowFromID(hWnd, GEOD_CURRELLIPSOID), szBuff);
  66.             WinPostMsg(hWnd, WM_COMMAND, MPFROMSHORT(usEllipsoid), 0L);
  67.             break;
  68.  
  69.         case WM_COMMAND:
  70.             switch(SHORT1FROMMP(mp1))
  71.             {
  72.                 case GEOD_AIRY:
  73.                 case GEOD_AUSNSA1969:
  74.                 case GEOD_BESSEL:
  75.                 case GEOD_CLARKE1866:
  76.                 case GEOD_CLARKE1880:
  77.                 case GEOD_EVEREST:
  78.                 case GEOD_FISCHER1960:
  79.                 case GEOD_FISCHER1960SA:
  80.                 case GEOD_FISCHER1968:
  81.                 case GEOD_HOUGH:
  82.                 case GEOD_INTL:
  83.                 case GEOD_IAU1968:
  84.                 case GEOD_KRASSOVSKIY:
  85.                 case GEOD_WGS1972:
  86.                     /* Update the ellipsoid (title) selected */
  87.                     WinCheckMenuItem(hwndMenu, usEllipsoid, FALSE);
  88.                     usEllipsoid = SHORT1FROMMP(mp1);
  89.                     WinCheckMenuItem(hwndMenu, usEllipsoid, TRUE);
  90.                     WinLoadString(hab, 0, SHORT1FROMMP(mp1), DEF_BUFF, szBuff);
  91.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_CURRELLIPSOID),
  92.                                      szBuff);
  93.  
  94.                     /* Update the equatorial radius */
  95.                     /* I *WISH* I knew an easier way to do this */
  96.                     xFrac = modf(el[SHORT1FROMMP(mp1) - GEOD_AIRY].EquatorialRadius[usMeasure - GEOD_NM], &xInt);
  97.                     commafmt(xBuff, DEF_BUFF, xInt);
  98.                     sprintf(FracBuff, "%0.3lf", xFrac);
  99.                     sprintf(szBuff, "%s.%s", xBuff, FracBuff+2);
  100.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_EQRAD), szBuff);
  101.  
  102.                     /* Update the polar radius */
  103.                     xFrac = modf(el[SHORT1FROMMP(mp1) - GEOD_AIRY].PolarRadius[usMeasure - GEOD_NM], &xInt);
  104.                     commafmt(xBuff, DEF_BUFF, xInt);
  105.                     sprintf(FracBuff, "%0.3lf", xFrac);
  106.                     sprintf(szBuff, "%s.%s", xBuff, FracBuff+2);
  107.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_POLRAD), szBuff);
  108.  
  109.                     /* Update the mean radius */
  110.                     xFrac = modf(el[SHORT1FROMMP(mp1) - GEOD_AIRY].MeanRadius[usMeasure - GEOD_NM], &xInt);
  111.                     commafmt(xBuff, DEF_BUFF, xInt);
  112.                     sprintf(FracBuff, "%0.3lf", xFrac);
  113.                     sprintf(szBuff, "%s.%s", xBuff, FracBuff+2);
  114.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_MEANRAD), szBuff);
  115.  
  116.                     /* Update the flattening */
  117.                     sprintf(szBuff, "%1.9lf",
  118.                         el[SHORT1FROMMP(mp1) - GEOD_AIRY].Flattening);
  119.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_FLAT), szBuff);
  120.  
  121.                     /* Update the eccentricity */
  122.                     sprintf(szBuff, "%1.9lf",
  123.                         el[SHORT1FROMMP(mp1) - GEOD_AIRY].Eccentricity);
  124.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_ECC), szBuff);
  125.  
  126.                     /* Update the eccentricity squared */
  127.                     sprintf(szBuff, "%1.9lf",
  128.                         el[SHORT1FROMMP(mp1) - GEOD_AIRY].Eccentricity2);
  129.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_ECC2), szBuff);
  130.                     return (MRESULT)TRUE;
  131.  
  132.                 case GEOD_NM:
  133.                 case GEOD_METERS:
  134.                     if (usMeasure == SHORT1FROMMP(mp1))
  135.                        return (MRESULT)TRUE;
  136.                     WinCheckMenuItem(hwndMenu, usMeasure, FALSE);
  137.                     usMeasure = SHORT1FROMMP(mp1);
  138.                     WinCheckMenuItem(hwndMenu, SHORT1FROMMP(mp1), TRUE);
  139.                     WinLoadString(hab, 0, SHORT1FROMMP(mp1), DEF_BUFF, szBuff);
  140.                     WinSetWindowText(WinWindowFromID(hWnd, GEOD_CURRMEASURE),
  141.                                      szBuff);
  142.                     /* Force an update of the information */
  143.                     WinPostMsg(hWnd, WM_COMMAND, MPFROMSHORT(usEllipsoid), 0L);
  144.                     return (MRESULT)TRUE;
  145.  
  146.                 case GEOD_ABOUT:
  147.                     WinDlgBox(HWND_DESKTOP, HWND_DESKTOP, AboutDlgProc,
  148.                               0, GEOD_ABOUTDIALOG, NULL);
  149.                     return (MRESULT)TRUE;
  150.  
  151.                 case GEOD_EXIT:
  152.                     WinPostMsg(hWnd, WM_QUIT, 0L, 0L);
  153.                     break;
  154.  
  155.                 default:
  156.                     return (MRESULT)TRUE;
  157.             }
  158.  
  159.         case WM_SYSCOMMAND:
  160.             /* Because this is a dialog and not a standard window (per se), */
  161.             /* we have to process this to ensure that the program is        */
  162.             /* exited properly (i.e. DBLCLK on the SysMenu). */
  163.             switch(SHORT1FROMMP(mp1))
  164.             {
  165.                 case SC_CLOSE:
  166.                     WinPostMsg(hWnd, WM_QUIT, 0L, 0L);
  167.                     break;
  168.             }
  169.  
  170.         default:
  171.             bHandled = FALSE;
  172.             break;
  173.     }
  174.  
  175.     if (!bHandled)
  176.         mReturn = WinDefDlgProc(hWnd, msg, mp1, mp2);
  177.  
  178.     return (mReturn);
  179. }
  180.  
  181. MRESULT EXPENTRY AboutDlgProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  182. {
  183.     switch (msg)
  184.     {
  185.         case WM_INITDLG:
  186.             CenterWindow(hwnd);
  187.             break;
  188.  
  189.         case WM_COMMAND:
  190.         case WM_SYSCOMMAND:
  191.             WinDismissDlg(hwnd, 0L);
  192.             break;
  193.     }
  194.     return(WinDefDlgProc(hwnd, msg, mp1, mp2));
  195. }
  196.  
  197. VOID CenterWindow(HWND hwnd)
  198. {
  199.     ULONG   ulScrWidth, ulScrHeight;
  200.     RECTL   Rectl;
  201.  
  202.     ulScrWidth  = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
  203.     ulScrHeight = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  204.  
  205.     WinQueryWindowRect(hwnd, &Rectl);
  206.     WinSetWindowPos(hwnd, HWND_TOP,
  207.                         (ulScrWidth - Rectl.xRight) / 2,
  208.                         (ulScrHeight - Rectl.yTop) / 2,
  209.                         0, 0, SWP_MOVE | SWP_ACTIVATE);
  210. }
  211.  
  212.  
  213. size_t commafmt(char   *buf,            /* Buffer for formatted string  */
  214.                 int     bufsize,        /* Size of buffer               */
  215.                 long    N)              /* Number to convert            */
  216. {
  217.         int len = 1, posn = 1, sign = 1;
  218.         char *ptr = buf + bufsize - 1;
  219.  
  220.         if (2 > bufsize)
  221.         {
  222. ABORT:          *buf = NUL;
  223.                 return 0;
  224.         }
  225.  
  226.         *ptr-- = NUL;
  227.         --bufsize;
  228.         if (0L > N)
  229.         {
  230.                 sign = -1;
  231.                 N = -N;
  232.         }
  233.  
  234.         for ( ; len <= bufsize; ++len, ++posn)
  235.         {
  236.                 *ptr-- = (char)((N % 10L) + '0');
  237.                 if (0L == (N /= 10L))
  238.                         break;
  239.                 if (0 == (posn % 3))
  240.                 {
  241.                         *ptr-- = ',';
  242.                         ++len;
  243.                 }
  244.                 if (len >= bufsize)
  245.                         goto ABORT;
  246.         }
  247.  
  248.         if (0 > sign)
  249.         {
  250.                 if (0 == bufsize)
  251.                         goto ABORT;
  252.                 *ptr-- = '-';
  253.                 ++len;
  254.         }
  255.  
  256.         strcpy(buf, ++ptr);
  257.         return (size_t)len;
  258. }
  259.